//
//  CKBPresenter.m
//  JJICar
//
//  Created by 朱维坚 on 16/3/17.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CKBPresenter.h"
#import "CKBApi.h"

@interface CKBPresenter ()<APICallBackProtocol>

@property (nonatomic,strong) CKBApi *api;

@end

@implementation CKBPresenter
DEF_SINGLETON(CKBPresenter)
@synthesize api=_api;
@synthesize adress;
@synthesize terminals;
@synthesize track;
- (void)dealloc
{
    self.api.delegate = nil;
    self.api = nil;
    INFO(@"%@>>>>>>>>>>>>>>>>>>>>out",[self class]);
    HM_SUPER_DEALLOC();
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.api = [CKBApi api];
        
    }
    return self;
}

typedef void (*__doFill)(id,SEL, NSArray *);

/**
 *  实现 fillThem:forSomething:方法；初始化数据、填充绑定的视图；
 *  需要实现doReset+xxx+Attributes:的方法
 *
 *  @param attributeBinded 操作数据成员数组
 *  @param something       操作别名 eg. login
 */
- (void)fillThem:(NSArray *)attributeBinded forSomething:(NSString *)something{
    
    NSString *sec = [NSString stringWithFormat:@"doReset%@Attributes:",something];
    SEL sel = NSSelectorFromString(sec);
    if ([self respondsToSelector:sel]) {
        Method method = class_getInstanceMethod( [self class], sel );
        __doFill  _doFill = (__doFill)method_getImplementation( method );
        _doFill(self,sel,attributeBinded);
    }
}

typedef BOOL (*__doVerity)(id,SEL, NSArray *);

/**
 *  实现 verityThem:forSomething:方法；验证数据有效性；
 *  需要实现doVerity+xxx+Attributes:的方法,xxx表示每个something参数值
 *
 *  @param attributeBinded 操作数据成员数组
 *  @param something       操作别名 eg. login
 *
 *  @return 成功或失败
 */
- (BOOL)verityThem:(NSArray *)attributeBinded forSomething:(NSString *)something{
    
    NSString *sec = [NSString stringWithFormat:@"doVerity%@Attributes:",something];
    SEL sel = NSSelectorFromString(sec);
    if ([self respondsToSelector:sel]) {
        Method method = class_getInstanceMethod( [self class], sel );
        __doVerity  _doVerity = (__doVerity)method_getImplementation( method );
        return _doVerity(self,sel,attributeBinded);
    }
    return YES;
}

typedef id (*__doSomething)(id,SEL, NSDictionary *);

/**
 *  实现 doSomething:attributes:方法；业务逻辑处理；
 *  需要实现do+xxx+Attributes:的方法,xxx表示每个something参数值
 *
 *  @param something       操作别名 eg. login
 *  @param attributeBinded 操作数据对象成员字典，外层用数组包一层
 *
 *  @return 实际对象
 */
- (id)doSomething:(NSString *)something attributes:(NSArray *)attributeBinded{
    NSDictionary *dic = [attributeBinded firstObject];
    
    NSString *sec = [NSString stringWithFormat:@"do%@Attributes:",something];
    SEL sel = NSSelectorFromString(sec);
    if ([self respondsToSelector:sel]) {
        Method method = class_getInstanceMethod( [self class], sel );
        __doSomething  _doSomething = (__doSomething)method_getImplementation( method );
        return _doSomething(self,sel,dic);
    }
    
    return nil;
}


typedef BOOL (*__doApi)(id,SEL, API * , NSString *, id, APIProcess, id, NSError *);

/**
 *  model层api数据的<APICallBackProtocol>协议回调；
 *  需要实现do+xxx+Api:doSome:bean:state:userinfo:error:的方法,xxx表示每个something参数值
 *
 *  @param api       实例化API子类
 *  @param something 操作别名 eg. login
 *  @param bean      网络数据实例化后的对象，详见Bean定义
 *  @param state     处理过程状态
 *  @param userinfo  附带用户信息
 *  @param error     错误
 */
- (void)api:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    
    BOOL respondtoController = YES;
    NSString *sec = [NSString stringWithFormat:@"do%@Api:doSome:bean:state:userinfo:error:",something];
    SEL sel = NSSelectorFromString(sec);
    if ([self respondsToSelector:sel]) {
        Method method = class_getInstanceMethod( [self class], sel );
        __doApi  _doApi = (__doApi)method_getImplementation( method );
        
        respondtoController = _doApi(self,sel,api,something,bean,state,userinfo,error);
        
    }
    
    if (respondtoController&&[self.delegate respondsToSelector:@selector(presenter:doSome:bean:state:error:)]) {
        [self.delegate presenter:self doSome:something bean:bean state:(PresenterProcess)state error:error];
    }
}

- (id)doReStore:(NSString *)something{
    id objc = [super doReStore:something];
    return objc==nil?@"":objc;
}
#pragma mark -



//http://test.ungps.com/clouddog/GetTermianlInfo?ID={0}&code={1}&loginName={3}
//http://weixin.ungps.com/clouddog/GetTermianlInfo
#pragma mark -

#pragma mark GetTermianlInfo
DEF_STRING(GetTermianlInfo,@"GetTermianlInfo")
DEF_STRING(GetTermianlInfoIO_loginName,@"GetTermianlInfoIO_loginName")
DEF_STRING(GetTermianlInfoIO_code,@"GetTermianlInfoIO_code")
- (void)doResetGetTermianlInfoAttributes:(NSArray *)attributeBinded{
    
}

- (BOOL)doVerityGetTermianlInfoAttributes:(NSArray*)attributeBinded{
    NSString *  username = [self doReStore:self.GetTermianlInfoIO_loginName];
    if (![username notEmpty]&&[attributeBinded containsObject:self.GetTermianlInfoIO_loginName]) {
        [self showMessageTip:@"温馨提示" detail:@"您没有登录名" timeOut:1.f];
        return NO;
    }
    return YES;
}

- (id)doGetTermianlInfoAttributes:(NSDictionary *)attributeBinded{
    NSString *  ID = @"";
    NSString *  code = @"chekongbao";
    NSString *  loginName = [self doReStore:self.GetTermianlInfoIO_loginName];
    
#ifdef DEBUG

//    loginName = @"13489184848";
    
#endif
    
    NSDictionary *dic = @{
                          @"ID":ID,
                          @"code":code,
                          @"loginName":loginName
                          };
    [self.api postOrGet:NO paras:dic to:CKBURL(@"clouddog/GetTermianlInfo") alias:[self GetTermianlInfo] encrypt:NO asData:dataTypeAsPara form:self];
    return nil;
}

- (BOOL)doGetTermianlInfoApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state==APIProcessSucced) {
        
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"获取设备列表" timeOut:2.f];
        self.terminals = bean.resultObject;
        
    }else if (state==APIProcessLoading) {
        [self showMessageTip:nil detail:@"获取设备列表..." timeOut:30.f];
    }else{
        [self showMessageTip:nil detail:bean.Message timeOut:2.f];
    }

    return YES;
}


//http://test.ungps.com/command/GetLastGpsItem?terminalToken={0}
#pragma mark - GetLastGpsItem
DEF_STRING(GetLastGpsItem,@"GetLastGpsItem")
DEF_STRING(GetLastGpsItemIO_terminalToken,@"GetLastGpsItemIO_terminalToken")
- (void)doResetGetLastGpsItemAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVerityGetLastGpsItemAttributes:(NSArray*)attributeBinded{
    
    return YES;
}

- (id)doGetLastGpsItemAttributes:(NSDictionary *)attributeBinded{
    NSString * terminalToken = [self doReStore:self.GetLastGpsItemIO_terminalToken];
    
    NSDictionary *dic = @{
                          @"terminalToken":terminalToken,
                          @"ip":@"117.89.35.58"
                          };
    
//    [self.api postOrGet:NO paras:dic to:CKBURL(@"command/GetLastGpsItem") alias:[self GetLastGpsItem] encrypt:NO asData:dataTypeAsPara form:self];
    [self.api postOrGet:NO paras:dic to:@"http://apis.baidu.com/apistore/iplookupservice/iplookup" alias:[self GetLastGpsItem] encrypt:NO asData:dataTypeAsPara form:self];
    return nil;
}
- (BOOL)doGetLastGpsItemApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state==APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"正在定位中" timeOut:2.f];
        self.adress = bean.resultObject;
        
    }else if (state==APIProcessLoading) {
        [self showMessageTip:nil detail:@"获取位置中..." timeOut:30.f];
    }else{
        [self showMessageTip:nil detail:bean.Message timeOut:2.f];
    }
    
    return YES;
}

//http://test.ungps.com/command/GetGpsItemList?terminalToken={0}&StartTime={1}&EndTime={2}&Speed={3}
#pragma mark - GetGpsItemList
DEF_STRING(GetGpsItemList,@"GetGpsItemList")
DEF_STRING(GetGpsItemListIO_terminalToken,@"GetGpsItemListIO_terminalToken")
DEF_STRING(GetGpsItemListIO_StartTime,@"GetGpsItemListIO_StartTime")
DEF_STRING(GetGpsItemListIO_EndTime,@"GetGpsItemListIO_EndTime")
DEF_STRING(GetGpsItemListIO_Speed,@"GetGpsItemListIO_Speed")
- (void)doResetGetGpsItemListAttributes:(NSArray *)attributeBinded{
    
}

- (BOOL)doVerityGetGpsItemListAttributes:(NSArray*)attributeBinded{
    
    return YES;
}

- (id)doGetGpsItemListAttributes:(NSDictionary *)attributeBinded{
    NSString *terminalToken = [self doReStore:self.GetGpsItemListIO_terminalToken];
    NSString *StartTime = [self doReStore:self.GetGpsItemListIO_StartTime];
    NSString *EndTime = [self doReStore:self.GetGpsItemListIO_EndTime];
    NSString *Speed = [self doReStore:self.GetGpsItemListIO_Speed];
    NSDictionary *dic = @{
                          @"terminalToken":terminalToken,
                          @"StartTime":StartTime,
                          @"EndTime":EndTime,
                          @"Speed":Speed
                          };
    
    [self.api postOrGet:NO paras:dic to:CKBURL(@"command/GetLastGpsItem")alias:[self GetLastGpsItem] encrypt:NO asData:dataTypeAsPara form:self];
    return nil;
}
- (BOOL)doGetLastGpsItemListApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"正在查询..." timeOut:2.f];
        self.track = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"正在查询中..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"没有轨迹..." timeOut:2.f];
    }
    return YES;
}
// http://121.40.34.252:7156/QueryAreaSetting?terminalKey={0}
#pragma mark - QueryAreaSetting
DEF_STRING(QueryAreaSetting,@"QueryAreaSetting")
DEF_STRING(QueryAreaSettingIO_terminalKey,@"QueryAreaSettingIO_terminalKey")
- (void)doResetQueryAreaSettingAttributes:(NSArray *)attributeBinded{
    
}

- (BOOL)doVerityQueryAreaSettingAttributes:(NSArray*)attributeBinded{
    NSString *  terminalKey = [self doReStore:self.QueryAreaSettingIO_terminalKey];
    
    if (![terminalKey notEmpty]&&[attributeBinded containsObject:self.QueryAreaSettingIO_terminalKey]) {
        [self showMessageTip:nil detail:@"没有terminalKey" timeOut:1.5f];
        return NO;
    }
    return YES;
}

- (id)doQueryAreaSettingAttributes:(NSDictionary *)attributeBinded{
    NSString *terminalKey = [self doReStore:self.QueryAreaSettingIO_terminalKey];
    NSDictionary *dic = @{
                          @"terminalKey":terminalKey
                          };
    [self.api postOrGet:NO paras:dic to:CKBAreaCmdURL(@"QueryAreaSetting") alias:[self QueryAreaSetting] encrypt:NO asData:dataTypeAsPara form:self];
    return nil;
}
- (BOOL)doQueryAreaSettingApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"正在为您查询报警设置" timeOut:2.f];
        self.setting = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"正在查询中..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }

    return YES;
}

//http://121.40.34.252:7156/DelAreaSetting?terminalKey={0}
#pragma mark DelAreaSetting
DEF_STRING(DelAreaSetting,@"DelAreaSetting")
DEF_STRING(DelAreaSettingIO_terminalKey,@"DelAreaSettingIO_terminalKey")
- (void)doResetDelAreaSettingAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVerityDelAreaSettingAttributes:(NSArray*)attributeBinded{
    
    return YES;
}
- (id)doDelAreaSettingAttributes:(NSDictionary *)attributeBinded{
    
    NSString *terminalKey = [self doReStore:self.DelAreaSettingIO_terminalKey];
    NSDictionary *dic = @{
                          @"terminalKey":terminalKey,
                          };
    [self.api postOrGet:YES paras:dic to:@"http://121.40.34.252:7156/DelAreaSetting" alias:[self DelAreaSetting] encrypt:NO asData:dataTypeAsData form:self];
    return nil; 
}
- (BOOL)doDelAreaSettingApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"正在删除..." timeOut:2.f];
        self.delSetting = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"正在删除..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }
    return YES;
}

//http://121.40.34.252:7156/AreaSetting?terminalKey={0}&areaType={1}&alarmType={2}&timeType={3}&lonlatJsonArray={4}&radius={5}&areaName={6}&offsetLon={7}&offsetLat={8}
#pragma mark AreaSetting
DEF_STRING(AreaSetting,@"AreaSetting")
DEF_STRING(AreaSettingIO_radius,@"AreaSettingIO_radius")
DEF_STRING(AreaSettingIO_offsetLon,@"AreaSettingIO_offsetLon")
DEF_STRING(AreaSettingIO_offsetLat,@"AreaSettingIO_offsetLat")
DEF_STRING(AreaSettingIO_timeType,@"AreaSettingIO_timeType")
DEF_STRING(AreaSettingIO_lonlatJsonArray,@"AreaSettingIO_lonlatJsonArray")
DEF_STRING(AreaSettingIO_areaType,@"AreaSettingIO_areaType")
DEF_STRING(AreaSettingIO_terminalKey,@"AreaSettingIO_terminalKey")
DEF_STRING(AreaSettingIO_areaName,@"AreaSettingIO_areaName")
DEF_STRING(AreaSettingIO_alarmType,@"AreaSettingIO_alarmType")
- (void)doResetAreaSettingAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVerityAreaSettingAttributes:(NSArray*)attributeBinded{
    
    return YES;
}

- (id)doAreaSettingAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  radius = [self doReStore:self.AreaSettingIO_radius];
    NSString *  offsetLon = [self doReStore:self.AreaSettingIO_offsetLon];
    NSString *  offsetLat = [self doReStore:self.AreaSettingIO_offsetLat];
    NSString *  timeType = [self doReStore:self.AreaSettingIO_timeType];
    NSString *  lonlatJsonArray = [self doReStore:self.AreaSettingIO_lonlatJsonArray];
    NSString *  areaType = [self doReStore:self.AreaSettingIO_areaType];
    NSString *  terminalKey = [self doReStore:self.AreaSettingIO_terminalKey];
    NSString *  areaName = [self doReStore:self.AreaSettingIO_areaName];
    NSString *  alarmType = [self doReStore:self.AreaSettingIO_alarmType];
    NSDictionary *dic = @{
                          @"radius":radius,
                          @"offsetLon":offsetLon,
                          @"offsetLat":offsetLat,
                          @"timeType":timeType,
                          @"lonlatJsonArray":lonlatJsonArray,
                          @"areaType":areaType,
                          @"terminalKey":terminalKey,
                          @"areaName":areaName,
                          @"alarmType":alarmType,
                          };
    [self.api postOrGet:YES paras:dic to:@"http://121.40.34.252:7156/AreaSetting" alias:[self AreaSetting] encrypt:NO asData:dataTypeAsData form:self];
    return nil; 
} 
- (BOOL)doAreaSettingApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"设置提交中..." timeOut:2.f];
        self.AreaResult = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"正在提交..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }
    return YES;
}
//http://weixin.ungps.com/clouddog/DeviceBinding?loginName={0}&termianlNumber={1}&simCard={2}&code={3}&termianlType={4}
#pragma mark DeviceBinding
DEF_STRING(DeviceBinding,@"DeviceBinding")
DEF_STRING(DeviceBindingIO_termianlNumber,@"DeviceBindingIO_termianlNumber")
DEF_STRING(DeviceBindingIO_code,@"DeviceBindingIO_code")
DEF_STRING(DeviceBindingIO_simCard,@"DeviceBindingIO_simCard")
DEF_STRING(DeviceBindingIO_loginName,@"DeviceBindingIO_loginName")
DEF_STRING(DeviceBindingIO_termianlType,@"DeviceBindingIO_termianlType")
- (void)doResetDeviceBindingAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVerityDeviceBindingAttributes:(NSArray*)attributeBinded{
    NSString *  termianlNumber = [self doReStore:self.DeviceBindingIO_termianlNumber];
    NSString *  termianlType = [self doReStore:self.DeviceBindingIO_termianlType];
    NSString *  simCard = [self doReStore:self.DeviceBindingIO_simCard];
    
    if (![termianlType notEmpty]&&[attributeBinded containsObject:self.DeviceBindingIO_termianlType]) {
        [self showMessageTip:nil detail:@"请填写型号" timeOut:1.f];
        return NO;
    }
    if (![termianlNumber notEmpty]&&[attributeBinded containsObject:self.DeviceBindingIO_termianlNumber]) {
        [self showMessageTip:nil detail:@"请填写串号" timeOut:1.f];
        return NO;
    }
    if (![simCard notEmpty]&&[attributeBinded containsObject:self.DeviceBindingIO_simCard]) {
        [self showMessageTip:nil detail:@"请填写卡号" timeOut:1.f];
        return NO;
    }

    return YES;
}
- (id)doDeviceBindingAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  termianlNumber = [self doReStore:self.DeviceBindingIO_termianlNumber];
    NSString *  code = [self doReStore:self.DeviceBindingIO_code];
    NSString *  simCard = [self doReStore:self.DeviceBindingIO_simCard];
    NSString *  loginName = [self doReStore:self.DeviceBindingIO_loginName];
    NSString *  termianlType = [self doReStore:self.DeviceBindingIO_termianlType];
    NSDictionary *dic = @{
                          @"termianlNumber":termianlNumber,
                          @"code":code,
                          @"simCard":simCard,
                          @"loginName":loginName,
                          @"termianlType":termianlType,
                          };
    [self.api postOrGet:YES paras:dic to:CKBURL(@"clouddog/DeviceBinding") alias:[self DeviceBinding] encrypt:NO asData:dataTypeAsData form:self];
    return nil; 
}
- (BOOL)doDeviceBindingApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"绑定成功" timeOut:2.f];
        self.TerminalInfo = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"正在绑定..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }
    return YES;
}

#pragma mark UpdateTermianlRemark
DEF_STRING(UpdateTermianlRemark,@"UpdateTermianlRemark")
DEF_STRING(UpdateTermianlRemarkIO_token,@"UpdateTermianlRemarkIO_token")
DEF_STRING(UpdateTermianlRemarkIO_remark,@"UpdateTermianlRemarkIO_remark")
#pragma mark UpdateTermianlRemark methd
- (void)doResetUpdateTermianlRemarkAttributes:(NSArray *)attributeBinded{
    /*1、可以初始化视图
	2、可以初始化数据
	使用者均可使用[ fillThem:forsomething:]初始化操作
	UITextField *input = [self getOutput:self.xxxIO_xxx];
	input.txt = @"test"
	*/
}

- (BOOL)doVerityUpdateTermianlRemarkAttributes:(NSArray*)attributeBinded{
/*1、进行数据有效性判断
2、给出必要的提示或不提示
使用者均可使用[ verityThem:forsomething:]进行判断
*/
    NSString *  token = [self doReStore:self.UpdateTermianlRemarkIO_token];
    NSString *  remark = [self doReStore:self.UpdateTermianlRemarkIO_remark];
    
    if (![token notEmpty]&&[attributeBinded containsObject:self.UpdateTermianlRemarkIO_token]) {
        [self showMessageTip:nil detail:@"没有token" timeOut:1.5f];
        return NO;
    }
    
    if (![remark notEmpty]&&[attributeBinded containsObject:self.UpdateTermianlRemarkIO_remark]) {
        [self showMessageTip:nil detail:@"没有remark" timeOut:1.5f];
        return NO;
    }
    return YES;
}

- (id)doUpdateTermianlRemarkAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  token = [self doReStore:self.UpdateTermianlRemarkIO_token];
    NSString *  remark = [self doReStore:self.UpdateTermianlRemarkIO_remark];
    
#ifdef DEBUG
    token = @"";
    remark = @"";
#endif
    
    NSDictionary *dic = @{
                          @"token":token,
                          @"remark":remark,
                          };
    return [self.api postOrGet:YES paras:dic to:CKBURL(@"clouddog/UpdateTermianlRemark") alias:[self UpdateTermianlRemark] encrypt:NO asData:dataTypeAsData form:self];
}


- (BOOL)doUpdateTermianlRemarkApi:(API *)api doSome:(NSString *)something bean:(id)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    
    if (state == APIProcessSucced||state == APIProcessOldData) {
        
        [self showMessageTip:nil detail:@"备注成功..." timeOut:1.5f];
        
    }else if (state == APIProcessLoading) {
        [self showMessageTip:nil detail:@"正在备注。。。" timeOut:30.f];
    }else if (state == APIProcessTimeOut) {
        [self showMessageTip:nil detail:@"超时" timeOut:1.5];
    }else if (state == APIProcessFailed) {
        [self showMessageTip:nil detail:@"失败" timeOut:1.5f];
    }
    return YES;
}


//http://weixin.ungps.com/clouddog/DeviceUnBinding?loginName={0}&token={1}&code={2}
 #pragma mark - DeviceUnBinding
DEF_STRING(DeviceUnBinding,@"DeviceUnBinding")
DEF_STRING(DeviceUnBindingIO_token,@"DeviceUnBindingIO_token")
DEF_STRING(DeviceUnBindingIO_loginName,@"DeviceUnBindingIO_loginName")
DEF_STRING(DeviceUnBindingIO_code,@"DeviceUnBindingIO_code")
#pragma mark DeviceUnBinding methd
- (void)doResetDeviceUnBindingAttributes:(NSArray *)attributeBinded{
    
    /*1、可以初始化视图
      2、可以初始化数据
      使用者均可使用[ fillThem:forsomething:]初始化操作
      UITextField *input = [self getOutput:self.xxxIO_xxx];
      nput.txt = @"test"*/
}

- (BOOL)doVerityDeviceUnBindingAttributes:(NSArray*)attributeBinded{
    
    /*1、进行数据有效性判断
     2、给出必要的提示或不提示
     使用者均可使用[ verityThem:forsomething:]进行判断
     */
    NSString *  token = [self doReStore:self.DeviceUnBindingIO_token];
    NSString *  loginName = [self doReStore:self.DeviceUnBindingIO_loginName];
    NSString *  code = [self doReStore:self.DeviceUnBindingIO_code];
    
    if (![token notEmpty]&&[attributeBinded containsObject:self.DeviceUnBindingIO_token]) {
        [self showMessageTip:nil detail:@"没有token" timeOut:1.5f];
        return NO;
    }
    
    if (![loginName notEmpty]&&[attributeBinded containsObject:self.DeviceUnBindingIO_loginName]) {
        [self showMessageTip:nil detail:@"没有loginName" timeOut:1.5f];
        return NO;
    }
    
    if (![code notEmpty]&&[attributeBinded containsObject:self.DeviceUnBindingIO_code]) {
        [self showMessageTip:nil detail:@"没有code" timeOut:1.5f];
        return NO;
    }
    return YES;
}

- (id)doDeviceUnBindingAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  token = [self doReStore:self.DeviceUnBindingIO_token];
    NSString *  loginName = [self doReStore:self.DeviceUnBindingIO_loginName];
    NSString *  code = [self doReStore:self.DeviceUnBindingIO_code];
    
#ifdef DEBUG
    token = @"";
    loginName = @"";
    code = @"";
#endif
    
    NSDictionary *dic = @{
                          @"token":token,
                          @"loginName":loginName,
                          @"code":code,
                          };
    return [self.api postOrGet:YES paras:dic to:CKBURL(@"clouddog/DeviceUnBinding") alias:[self DeviceUnBinding] encrypt:NO asData:dataTypeAsData form:self];
}

- (BOOL)doDeviceUnBindingApi:(API *)api doSome:(NSString *)something bean:(id)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    
    if (state == APIProcessSucced||state == APIProcessOldData) {
        [self showMessageTip:nil detail:@"解除绑定成功" timeOut:1.5f];
//        [self doGetGpsItemListAttributes:[self GetGpsItemList] ];
    }else if (state == APIProcessLoading) {
        [self showMessageTip:nil detail:@"正在解除绑定" timeOut:30.f];
    }else if (state == APIProcessTimeOut) {
        [self showMessageTip:nil detail:@"超时" timeOut:1.5];
    }else if (state == APIProcessFailed) {
        [self showMessageTip:nil detail:@"解除失败失败" timeOut:1.5f];
    }
    return YES;
}

//http://weixin.ungps.com/clouddog/DeviceUnBinding?loginName={0}&token={1}&code={2}
//http://weixin.ungps.com/qiye/GetPublicParam
#pragma mark GetPublicParam
DEF_STRING(GetPublicParam,@"GetPublicParam")
DEF_STRING(GetPublicParamIO_TypeName,@"GetPublicParamIO_TypeName")
DEF_STRING(GetPublicParamIO_SIM,@"GetPublicParamIO_SIM")
DEF_STRING(GetPublicParamIO_PublicNo,@"GetPublicParamIO_PublicNo")

- (void)doResetGetPublicParamAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVerityGetPublicParamAttributes:(NSArray*)attributeBinded{
    
    return YES;
}
- (id)doGetPublicParamAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  TypeName = [self doReStore:self.GetPublicParamIO_TypeName];
    NSString *  SIM = [self doReStore:self.GetPublicParamIO_SIM];
    NSString *  PublicNo = [self doReStore:self.GetPublicParamIO_PublicNo];
    NSDictionary *dic = @{
                          @"TypeName":TypeName,
                          @"SIM":SIM,
                          @"PublicNo":PublicNo,
                          };
    [self.api postOrGet:YES paras:dic to:CKBURL(@"qiye/GetPublicParam") alias:[self GetPublicParam] encrypt:NO asData:dataTypeAsData form:self];
    return nil;
}
- (BOOL)doGetPublicParamApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
         [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"获取参数成功..." timeOut:2.f];
        self.paramSetting = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"参数获取中..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }
    return YES;
}
//http://weixin.ungps.com/command/SendCommand
#pragma mark SendCommand
DEF_STRING(SendCommand,@"SendCommand")
DEF_STRING(SendCommandIO_terminalToken,@"SendCommandIO_terminalToken")
DEF_STRING(SendCommandIO_commandType,@"SendCommandIO_commandType")
DEF_STRING(SendCommandIO_json,@"SendCommandIO_json")
- (void)doResetSendCommandAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVeritySendCommandAttributes:(NSArray*)attributeBinded{
    
    return YES;
}
- (id)doSendCommandAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  terminalToken = [self doReStore:self.SendCommandIO_terminalToken];
    NSString *  commandType = [self doReStore:self.SendCommandIO_commandType];
    NSString *  json = [self doReStore:self.SendCommandIO_json];
    NSDictionary *dic = @{
                          @"terminalToken":terminalToken,
                          @"commandType":commandType,
                          @"json":json,
                          };
    [self.api postOrGet:YES paras:dic to:CKBURL(@"command/SendCommand") alias:[self SendCommand] encrypt:NO asData:dataTypeAsData form:self];
    return nil; 
}
- (BOOL)doSendCommandApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        self.SendCommandReply = bean.resultObject;
        if ([self.SendCommandReply.CommandState integerValue] == 10) {
            [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"终端未应答" timeOut:2.f];
        }else{
            if ([self.SendCommandReply.CommandState integerValue] == 0) {
                [self showMessageTip:nil detail:@"操作成功" timeOut:2.f];
            }else if ([self.SendCommandReply.CommandState integerValue] == 1){
                [self showMessageTip:nil detail:@"终端未连线" timeOut:2.f];
            }else if ([self.SendCommandReply.CommandState integerValue] == 2){
                [self showMessageTip:nil detail:@"Error" timeOut:1.f];
            }else if ([self.SendCommandReply.CommandState integerValue] == 3){
                [self showMessageTip:nil detail:@"超时" timeOut:1.f];
            }else if ([self.SendCommandReply.CommandState integerValue] == 5){
                [self showMessageTip:nil detail:@"设备不支持" timeOut:2.f];
            }else if ([self.SendCommandReply.CommandState integerValue] == 6){
                [self showMessageTip:nil detail:@"协议错误" timeOut:2.f];
            }else if ([self.SendCommandReply.CommandState integerValue] == 8){
                [self showMessageTip:nil detail:@"终端返回失败" timeOut:2.f];
            }
        }
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"正在为您发送请求" timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }

    return YES;
}
//http://weixin.ungps.com/command/SearchMediaFileLastTimeItem
#pragma mark SearchMediaFileLastTimeItem
DEF_STRING(SearchMediaFileLastTimeItem,@"SearchMediaFileLastTimeItem")
DEF_STRING(SearchMediaFileLastTimeItemIO_terminalToken,@"SearchMediaFileLastTimeItemIO_terminalToken")
DEF_STRING(SearchMediaFileLastTimeItemIO_mediaType,@"SearchMediaFileLastTimeItemIO_mediaType")

- (void)doResetSearchMediaFileLastTimeItemAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVeritySearchMediaFileLastTimeItemAttributes:(NSArray*)attributeBinded{
    
    return YES;
}
- (id)doSearchMediaFileLastTimeItemAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  terminalToken = [self doReStore:self.SearchMediaFileLastTimeItemIO_terminalToken];
    NSString *  mediaType = [self doReStore:self.SearchMediaFileLastTimeItemIO_mediaType];
    NSDictionary *dic = @{
                          @"terminalToken":terminalToken,
                          @"mediaType":mediaType,
                          };
    [self.api postOrGet:YES paras:dic to:CKBURL(@"command/SearchMediaFileLastTimeItem") alias:[self SearchMediaFileLastTimeItem] encrypt:NO asData:dataTypeAsData form:self];
    return nil;
}
- (BOOL)doSearchMediaFileLastTimeItemApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        if (self.SendCommandReply.CmdSendTime<self.lastTimeItem.ReceiveTime) {
            [self doSearchMediaFilesByTimesAttributes:self.SearchMediaFilesByTimes];
        }
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }


    return YES;
}

//http://weixin.ungps.com/command/SearchMediaFiles
#pragma mark -SearchMediaFilesByTimes
DEF_STRING(SearchMediaFilesByTimes,@"SearchMediaFilesByTimes")
DEF_STRING(SearchMediaFilesByTimesIO_mediaType,@"SearchMediaFilesByTimesIO_mediaType")
DEF_STRING(SearchMediaFilesByTimesIO_times,@"SearchMediaFilesByTimesIO_times")
DEF_STRING(SearchMediaFilesByTimesIO_ascOrDes,@"SearchMediaFilesByTimesIO_ascOrDes")
DEF_STRING(SearchMediaFilesByTimesIO_terminalToken,@"SearchMediaFilesByTimesIO_terminalToken")
- (void)doResetSearchMediaFilesByTimesAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVeritySearchMediaFilesByTimesAttributes:(NSArray*)attributeBinded{
    
    return YES;
}
- (id)doSearchMediaFilesByTimesAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  mediaType = [self doReStore:self.SearchMediaFilesByTimesIO_mediaType];
    NSString *  times = [self doReStore:self.SearchMediaFilesByTimesIO_times];
    NSString *  ascOrDes = [self doReStore:self.SearchMediaFilesByTimesIO_ascOrDes];
    NSString *  terminalToken = [self doReStore:self.SearchMediaFilesByTimesIO_terminalToken];
    NSDictionary *dic = @{
                          @"mediaType":mediaType,
                          @"times":times,
                          @"ascOrDes":ascOrDes,
                          @"terminalToken":terminalToken,
                          };
    [self.api postOrGet:YES paras:dic to:CKBURL(@"command/SearchMediaFiles") alias:[self SearchMediaFilesByTimes] encrypt:NO asData:dataTypeAsData form:self];
    return nil; 
}
- (BOOL)doSearchMediaFilesByTimesApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"正在为您查询" timeOut:2.f];
        self.MediaItem = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"查询中..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }

    
    return YES;
}

//http://weixin.ungps.com/command/SearchMediaFiles
#pragma mark - SearchMediaFiles
DEF_STRING(SearchMediaFiles,@"SearchMediaFiles")
DEF_STRING(SearchMediaFilesIO_startTime ,@"SearchMediaFilesIO_startTime ")
DEF_STRING(SearchMediaFilesIO_mediaType,@"SearchMediaFilesIO_mediaType")
DEF_STRING(SearchMediaFilesIO_ascOrDes,@"SearchMediaFilesIO_ascOrDes")
DEF_STRING(SearchMediaFilesIO_terminalToken,@"SearchMediaFilesIO_terminalToken")
DEF_STRING(SearchMediaFilesIO_endTime ,@"SearchMediaFilesIO_endTime ")
- (void)doResetSearchMediaFilesAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVeritySearchMediaFilesAttributes:(NSArray*)attributeBinded{
    
    return YES;
}
- (id)doSearchMediaFilesAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  startTime  = [self doReStore:self.SearchMediaFilesIO_startTime ];
    NSString *  mediaType = [self doReStore:self.SearchMediaFilesIO_mediaType];
    NSString *  ascOrDes = [self doReStore:self.SearchMediaFilesIO_ascOrDes];
    NSString *  terminalToken = [self doReStore:self.SearchMediaFilesIO_terminalToken];
    NSString *  endTime  = [self doReStore:self.SearchMediaFilesIO_endTime ];
    NSDictionary *dic = @{
                          @"startTime ":startTime ,
                          @"mediaType":mediaType,
                          @"ascOrDes":ascOrDes,
                          @"terminalToken":terminalToken,
                          @"endTime ":endTime ,
                          };
    [self.api postOrGet:YES paras:dic to:CKBURL(@"command/SearchMediaFiles") alias:[self SearchMediaFiles] encrypt:NO asData:dataTypeAsData form:self];
    return nil; 
}
- (BOOL)doSearchMediaFilesApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"正在为您查询" timeOut:2.f];
        self.MeidaItemArry = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"查询中..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }

    
    return YES;
}

//http://weixin.ungps.com/command/GetCommandExecuteResult
#pragma mark GetCommandExecuteResult
DEF_STRING(GetCommandExecuteResult,@"GetCommandExecuteResult")
DEF_STRING(GetCommandExecuteResultIO_terminalToken,@"GetCommandExecuteResultIO_terminalToken")
DEF_STRING(GetCommandExecuteResultIO_commandType,@"GetCommandExecuteResultIO_commandType")
#pragma mark GetCommandExecuteResult
- (void)doResetGetCommandExecuteResultAttributes:(NSArray *)attributeBinded{
    
}
- (BOOL)doVerityGetCommandExecuteResultAttributes:(NSArray*)attributeBinded{
    
    return YES;
}
- (id)doGetCommandExecuteResultAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  terminalToken = [self doReStore:self.GetCommandExecuteResultIO_terminalToken];
    NSString *  commandType = [self doReStore:self.GetCommandExecuteResultIO_commandType];
    NSDictionary *dic = @{
                          @"terminalToken":terminalToken,
                          @"commandType":commandType,
                          };
    [self.api postOrGet:YES paras:dic to:CKBURL(@"command/GetCommandExecuteResult") alias:[self GetCommandExecuteResult] encrypt:NO asData:dataTypeAsData form:self];
    return nil; 
}
- (BOOL)doGetCommandExecuteResultApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    if (state == APIProcessSucced) {
        [self showMessageTip:nil detail:[bean.Message notEmpty]?bean.Message:@"正在为您查询" timeOut:2.f];
        self.SendCommandReply = bean.resultObject;
    }else if (state==APIProcessLoading){
        [self showMessageTip:nil detail:@"查询中..." timeOut:2.f];
    }else if (state == APIProcessFailed){
        [self showMessageTip:nil detail:@"" timeOut:2.f];
    }
    return YES;
}

#pragma mark GetCardInfo
DEF_STRING(GetCardInfo,@"GetCardInfo")
DEF_STRING(GetCardInfoIO_sim,@"GetCardInfoIO_sim")
DEF_STRING(GetCardInfoIO_searchKey,@"GetCardInfoIO_searchKey")
#pragma mark GetCardInfo methd
- (void)doResetGetCardInfoAttributes:(NSArray *)attributeBinded{
    
    /*1、可以初始化视图
     2、可以初始化数据
     使用者均可使用[ fillThem:forsomething:]初始化操作
     UITextField *input = [self getOutput:self.xxxIO_xxx];
     input.txt = @"test"
     */
}

- (BOOL)doVerityGetCardInfoAttributes:(NSArray*)attributeBinded{
    
    /*1、进行数据有效性判断
     2、给出必要的提示或不提示
     使用者均可使用[ verityThem:forsomething:]进行判断
     */
    
    NSString *  sim = [self doReStore:self.GetCardInfoIO_sim];
    NSString *  searchKey = [self doReStore:self.GetCardInfoIO_searchKey];
    
    if (![sim notEmpty]&&[attributeBinded containsObject:self.GetCardInfoIO_sim]) {
        [self showMessageTip:nil detail:@"没有sim" timeOut:1.5f];
        return NO;
    }
    
    if (![searchKey notEmpty]&&[attributeBinded containsObject:self.GetCardInfoIO_searchKey]) {
        [self showMessageTip:nil detail:@"没有searchKey" timeOut:1.5f];
        return NO;
    }
    
    return YES;
}

- (id)doGetCardInfoAttributes:(NSDictionary *)attributeBinded{
    
    NSString *  sim = [self doReStore:self.GetCardInfoIO_sim];
    NSString *  searchKey = [self doReStore:self.GetCardInfoIO_searchKey];
    
//#ifdef DEBUG
//    sim = @"";
//    searchKey = @"fxft_lcd";
//#endif
    
    NSDictionary *dic = @{
                          @"sim":sim,
                          @"searchKey":searchKey,
                          };
    return [self.api postOrGet:NO paras:dic to:CKBURL(@"clouddog/GetCardInfo")alias:[self GetCardInfo] encrypt:NO asData:dataTypeAsPara form:self];
}


- (BOOL)doGetCardInfoApi:(API *)api doSome:(NSString *)something bean:(CKBWebAPIResult*)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    
    if (state == APIProcessSucced||state == APIProcessOldData) {
        
        [self showMessageTip:nil detail:@"获取成功" timeOut:1.f];
        self.carInfo = bean.resultObject;
        
    }else if (state == APIProcessLoading) {
        [self showMessageTip:nil detail:@"卡号获取中" timeOut:20.f];
    }else if (state == APIProcessTimeOut) {
        [self showMessageTip:nil detail:bean.Message timeOut:1.5];
    }else if (state == APIProcessFailed) {
        [self showMessageTip:nil detail:bean.Message timeOut:1.5f];
    }
    return YES;
}
@end
